package com.fubowen.lepton.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.MapInfo
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.fubowen.lepton.bean.BookmarkBean
import kotlinx.coroutines.flow.Flow

@Dao
interface BookmarkDao {

    @Query("select * from BookmarkBean")
    fun all(): Flow<List<BookmarkBean>>

    @Query("select * from BookmarkBean where Id = :id")
    fun get(id: Int): BookmarkBean

    @Query("select *,(SELECT COUNT(*) FROM BookmarkBean a WHERE a.parentId = b.id) AS childCount from BookmarkBean b where parentId=:parentId")
    fun getByParentIdFlow(parentId: Int): Flow<List<BookmarkBean>>

    @Query("select *,(SELECT COUNT(*) FROM BookmarkBean a WHERE a.parentId = b.id) AS childCount from BookmarkBean b where parentId=:parentId")
    fun getByParentId(parentId: Int): List<BookmarkBean>

    /**
     * @return 插入数据的主键
     */
    @Insert
    fun set(bookmarkBean: BookmarkBean): Long

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun setOrReplace(bookmarkBean: BookmarkBean): Long

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun setOrIgnore(bookmarkBean: BookmarkBean): Long

    /**
     * @return 插入数据的主键集合
     */
    @Insert
    fun setList(vararg bookmarkBean: BookmarkBean): LongArray

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun setOrIgnore(list: List<BookmarkBean>): LongArray

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun setOrReplace(list: List<BookmarkBean>): LongArray

    /**
     * @return 成功更新的数据行数
     */
    @Update
    fun put(bookmarkBean: BookmarkBean): Int

    @Query("update BookmarkBean set parentId=:parentId where id in (:ids)")
    fun put(parentId: Int, ids: List<Int>): Int

    @Query("update BookmarkBean set parentId=:parentId where parentId =:id")
    fun put(parentId: Int, id: Int): Int

    /**
     * @return 成功删除的数据行数
     */
    @Delete
    fun del(bookmarkBean: BookmarkBean): Int

    @Query("delete from BookmarkBean where id in (:ids)")
    fun del(ids: List<Int>): Int

    @Query("DELETE FROM BookmarkBean")
    fun clear()
}